package com.example.myxm.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myxm.App;
import com.example.myxm.auth.LoginManager;
import com.example.myxm.databinding.ActivityLoginBinding;
import com.example.myxm.module.ResultBean;
import com.example.myxm.module.UserBean;

public class LoginActivity extends AppCompatActivity {
    ActivityLoginBinding   binding;
    private static final String TAG = "LoginActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = binding.etUsername.getText().toString();
                String password =  binding.etPassword.getText().toString();
                ResultBean<UserBean> user = App.getDbHelperStatic().loginUserBean(username, password);
                if (user.getCode() == LoginManager.RESULT_CODE){
                    Intent intent=new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    LoginManager.saveLogin(LoginActivity.this,user.getData()); //保存登录信息
                }else{
                    Toast.makeText(LoginActivity.this, user.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        });
        binding.btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this ,RegisterActivity.class);
                startActivity(intent);
            }
        });
    }
}